import {Request, Controller, Get, Param, Post, Query} from "@nestjs/common";
import {ApiOperation, ApiParam, ApiQuery, ApiResponse, ApiTags} from "@nestjs/swagger";
import {BooksService} from "./books.service";
import {PagingRequestDto} from "../../common/dto/paging.dto";
import {Public} from "../../common/decorators/public.decorators";
import {BooksDto, SearchBookDto} from "./books.dto";


@ApiTags("图书")
@Controller("books")
export class BooksController {

    constructor(
        private readonly bookService: BooksService
    ) {
    }

    @Get("getListByPage")
    @Public()
    @ApiOperation({summary: "获取图书列表(分页)"})
    @ApiQuery({
        type: PagingRequestDto
    })
    @ApiResponse({
        type: BooksDto
    })
    async get(
        @Query() paging: PagingRequestDto
    ) {
        const {pageNo, pageSize} = paging;
        return await this.bookService.getList(pageNo, pageSize);
    }

    @Get("search")
    @Public()
    @ApiOperation({summary: "根据名称搜索（作者，图书，分类，标签）"})
    @ApiQuery({name: "searchValue", description: "搜索内容", type: "string"})
    @ApiQuery({name: "size", description: "数据长度", type: "number"})
    async search(
        @Query("searchValue") searchValue: string,
        @Query("size") size: string | number
    ) {
        return await this.bookService.searchNodeByName(searchValue, Number(size));
    }

    @Get("searchBooks")
    @Public()
    @ApiOperation({summary: "根据名称搜索图书"})
    @ApiQuery({name: "searchValue", description: "搜索内容", type: "string"})
    @ApiQuery({name: "size", description: "数据长度", type: "number"})
    async searchBooks(
        @Query() searchBookDto: SearchBookDto,
    ) {
        const {searchValue, size} = searchBookDto
        return await this.bookService.searchBookByName(searchValue, Number(size));
    }

    @Post("setScore/:bookId/:score")
    @ApiOperation({summary: "给图书评分"})
    @ApiParam({name: "bookId", description: "图书id", type: "string"})
    @ApiParam({name: "score", description: "评分", type: "number"})
    async setScore(
        @Request() req,
        @Param("bookId") bookId: string,
        @Param("score") score: string | number
    ) {
        const {user: {userId}} = req;
        return await this.bookService.setScoreByBookId(userId, bookId, Number(score));
    }
}
